package com.rtu.web;
import com.alibaba.fastjson.JSONObject;
import java.sql.ResultSet;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.apache.commons.collections.map.HashedMap;
import org.apache.commons.lang3.StringUtils;
import org.apache.commons.lang3.time.DateFormatUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import com.rtu.dao.ILzRainGatherDataDao;
import com.rtu.dao.IRainGatherDataDao;
import com.rtu.domain.RainGatherData;
import com.rtu.domain.Station;
import com.rtu.dto.QuerySiphonDto;
import com.rtu.dto.QueryStationDto;
import com.rtu.dto.QueryStationRainReportDto;
import com.rtu.dto.RainWaterDto;
import com.rtu.dto.RainWaterReportDto;
import com.rtu.dto.response.ApiResponse;
import com.rtu.enums.AccessToken;
import com.rtu.enums.Code;
import com.rtu.enums.SDMappingEnum;
import com.rtu.service.ILzRainStationService;
import com.rtu.service.IStationService;
import com.rtu.utils.DateUtil;
import com.rtu.utils.HttpUtil;
import com.rtu.utils.JDBCUtil;
import com.rtu.utils.ResultSetMapper;

@Controller
@RequestMapping("/video")
public class VideoController {

	private static final Logger logger = LoggerFactory.getLogger(VideoController.class);
	
	private static final  String GET_TOKEN_URL ="https://open.ys7.com/api/lapp/token/get";
    //设备序列号
    private static final  String APPID ="C16523648";
    private static final  String APPKEY ="760526d363814cfc9bdfaf9ced3d162c";
    private static final  String APPSECRET ="18bab3a83d673b5e58d81337847bb579";
    private static AccessToken at=null;
  
    private static void getTokenTest() {
    	
    	String param ="appKey="+APPKEY+"&appSecret="+APPSECRET; 	
    	String result = HttpUtil.post(GET_TOKEN_URL,param);
    	System.out.println(result);
    	
        //JSONObject jsonObject = JSONObject.fromObject(result);
    	JSONObject jsonObject =(JSONObject) JSONObject.parse(result);
    	
        System.out.println(jsonObject);
    	
        String data = jsonObject.getString("data");
    	
    	JSONObject jsonObject2 =(JSONObject) JSONObject.parse(data);
    	String token = jsonObject2.getString("accessToken");   	
    	String expireIn=(String)jsonObject2.getString("expireTime");
    	Long expireIn2 =Long.parseLong(expireIn);
    	
    	at = new AccessToken(token,expireIn2,data);
    	
    	
    }
    @RequestMapping("/getAccessToken")
    @ResponseBody
    public ApiResponse getAccessToken(){
        ApiResponse apiResponse;
        try {
           
           // Map map = new HashedMap();            
            if(at==null || at.isExpired()) {
        		getTokenTest();
        	}        	
           // map.put("tokendata", at.getData());
        	
           /* if (map == null || map.get("data") == null) {
                apiResponse = ApiResponse.create(false,null, Code.NO_PROJECT.code,null);
                return apiResponse;
            }*/
            apiResponse = ApiResponse.create(true, Code.SUCCESS, at.getData());
        }catch (Exception e){
            logger.error("getStation",e);
            apiResponse = ApiResponse.create(false,e.getMessage(),Code.COMMON_SERVER_ERROR.code,null);
        }
        return apiResponse;
    }
	

    
}
